package org.jeecg.modules.flower.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.flower.entity.FlowerPlantingBase2;
import org.jeecg.modules.flower.entity.FlowerPlantingGreenhouse2;
import org.jeecg.modules.flower.mapper.FlowerPlantingGreenhouse2Mapper;
import org.jeecg.modules.flower.mapper.FlowerPlantingBase2Mapper;
import org.jeecg.modules.flower.service.IFlowerPlantingBase2Service;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 基地管理
 * @Author: jeecg-boot
 * @Date:   2022-12-02
 * @Version: V1.0
 */
@Service
public class FlowerPlantingBase2ServiceImpl extends ServiceImpl<FlowerPlantingBase2Mapper, FlowerPlantingBase2> implements IFlowerPlantingBase2Service {

	@Autowired
	private FlowerPlantingBase2Mapper flowerPlantingBase2Mapper;
	@Autowired
	private FlowerPlantingGreenhouse2Mapper flowerPlantingGreenhouse2Mapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(FlowerPlantingBase2 flowerPlantingBase2, List<FlowerPlantingGreenhouse2> flowerPlantingGreenhouse2List) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
//		flowerPlantingBase2.setCreateUser(sysUser.getRealname());
		flowerPlantingBase2Mapper.insert(flowerPlantingBase2);
		for(FlowerPlantingGreenhouse2 entity:flowerPlantingGreenhouse2List) {
			//外键设置
			entity.setParentId(flowerPlantingBase2.getId());
			flowerPlantingGreenhouse2Mapper.insert(entity);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(FlowerPlantingBase2 flowerPlantingBase2,List<FlowerPlantingGreenhouse2> flowerPlantingGreenhouse2List) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
		flowerPlantingBase2.setUpdateUser(sysUser.getRealname());
		flowerPlantingBase2Mapper.updateById(flowerPlantingBase2);
		
		//1.先删除子表数据
		flowerPlantingGreenhouse2Mapper.deleteByMainId(flowerPlantingBase2.getId());
		
		//2.子表数据重新插入
		for(FlowerPlantingGreenhouse2 entity:flowerPlantingGreenhouse2List) {
			//外键设置
			entity.setParentId(flowerPlantingBase2.getId());
			flowerPlantingGreenhouse2Mapper.insert(entity);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		flowerPlantingGreenhouse2Mapper.deleteByMainId(id);
		flowerPlantingBase2Mapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			flowerPlantingGreenhouse2Mapper.deleteByMainId(id.toString());
			flowerPlantingBase2Mapper.deleteById(id);
		}
	}
	
}
